package com.xx.ems.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.xx.ems.model.Action;
import com.xx.ems.model.Users;
import com.xx.ems.system.BaseController;

/**
 * @author xxx
 * @date 2014-2-25
 * @version 1.0
 * 描述: 用户登陆处理类
 */
@Controller
@RequestMapping(value="userLogin",method=RequestMethod.POST)
@SessionAttributes(types=Users.class,value="loginuser")
public class UserLoginController extends BaseController {
	/**
	 * 描述: 验证用户输入的信息是否正确
	 * @param username
	 * @param password
	 * @param response
	 * @return
	 * @date 2014-2-26
	 * 
	 * json格式：
	 * var user = {account:"1040112334",password:"123"};
	 * $.ajax({
	 * 		url:"/ems/userLogin/userCheck",
	 * 		type:"POST",
	 * 		data:user,
	 * 		dataType:"json",
	 * 		success:function(data){
	 * 			alert(data.result);
	 * 		}
	 * });
	 */
	@RequestMapping(value="/userCheck")
	public void userCheck(String account, String password, HttpServletRequest request, HttpServletResponse response, ModelMap modelmap){
		try {
			Users user = getUserLoginService().userCheck(account, getMd5().toMd5(password));
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			/**
			 * 判断输入的用户信息是否正确
			 */
			if (user != null) {
				json.put("result", "suc");
				json.put("status", "0");
				modelmap.addAttribute("loginuser", user);
			}else {
				json.put("status", "1");
				json.put("err_msg", "账号或密码错误！");
			}
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 用户注销 
	 * @param request
	 * @return
	 * @date 2014-3-8
	 */
	@RequestMapping(value="/userLoginOut")
	public String userLoginOut(HttpServletRequest request){
		request.getSession().setAttribute("loginuser", null);
		return "redirect:/login.jsp";
	}
	
	/**
	 * 描述: 进行页面跳转 
	 * @return
	 * @date 2014-3-7
	 */
	@RequestMapping(value="/userLogin")
	public String userLogin() {
		return "redirect:/index.jsp";
	}
	
	/**
	 * 描述: 获取用户的权限列表 
	 * @param account
	 * @param response
	 * @date 2014-3-7
	 */
	@RequestMapping(value="/getActionListByAccont")
	public void getActionListByAccont(String account, HttpServletResponse response) {
		try{
			/**
			 * 获取权限集
			 */
			Set<Action> actions = getUserLoginService().getActionListByAccont(account);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			/**
			 * 判断有无权限
			 */
			if(actions != null) {
				/**
				 * 这样处理是为了使数据传到前台时,格式为{"result":[{a1:"1",b1:"2"},{a2:"11",b2:"22"}]}
				 * 前台如果是用参数(如：data)来接受数据,则可以直接通过 data.result[0].a1 这种形式来获取 a1 的值
				 */
				List<Object> list = new ArrayList<Object>();
				for (Action action : actions){
					list.add(action.toHashMap());
				}
				json.put("result", list);
			}else {
				/**
				 * 没有相应的权限时,返回一个empty字符串
				 */
				json.put("result", "empty");
			}
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}